#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <iostream>

using namespace std;

static void sig_quit( int );
int main( int argc, char *argv[] )
{
	sigset_t newmask, oldmask, pendmask;

	if( signal( SIGQUIT, sig_quit ) == SIG_ERR ) {
		perror( "can't catch SIGQUIT" );
		exit( -1 );
	}

	// Block SIGQUIT and save current signal mask.
	sigemptyset( &newmask );
	sigaddset( &newmask, SIGQUIT );
	if( sigprocmask( SIG_BLOCK, &newmask, &oldmask ) < 0 )
		perror( "SIG_BLOCK error" );

	sleep( 5 );

	if( sigpending( &pendmask ) < 0 )
		perror( "sigpending error" );
	if( sigismember( &pendmask, SIGQUIT ) )
		cout << endl << "SIGQUIT pending" << endl;

	// Reset signal mask which unblocks SIGQUIT.
	if( sigprocmask( SIG_SETMASK, &oldmask, NULL ) < 0 )
		perror( "SIG_SETMASK error" );

	sleep( 5 );

	return 0;
}

static void sig_quit( int signo )
{
	cout << "cught SIGQUIT" << endl;
	if( signal( SIGQUIT, SIG_DFL ) == SIG_ERR )
		perror( "can't reset SIGQUIT" );
}
